package com.mconnect.msurvey.controller;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;

import com.mconnect.msurvey.model.PriceList;
import com.mconnect.msurvey.model.PriceListRecommend;
import com.mconnect.msurvey.model.PriceListSurvey;
import com.mconnect.msurvey.util.HibernateUtil;

public class PriceListManager extends HibernateUtil{
	
	public PriceList savePriceList(String [] price1Survey, String[] begin, String[] end, String[] price1Recommend,  String[] beginRecommend, String[] endRecommend){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	    session.getTransaction().begin();
	    
	    PriceList price = new PriceList();
	    List<PriceListSurvey> responderList = new ArrayList<PriceListSurvey>();
	    List<PriceListRecommend> recommendList = new ArrayList<PriceListRecommend>();
	    for(int i=1; i<=price1Survey.length -1; i++){
	    	PriceListSurvey priceSurvey = new PriceListSurvey();
	    	priceSurvey.setBegin(Integer.parseInt(begin[i]));
	    	priceSurvey.setEnd(Integer.parseInt(end[i]));
	    	priceSurvey.setPrice1Survey(Double.parseDouble(price1Survey[i]));
	    	responderList.add(priceSurvey);
	    }
	    
	    for(int i=1; i<=price1Recommend.length -1; i++){
	    	PriceListRecommend priceRecommend = new PriceListRecommend();
	    	priceRecommend.setBeginRecommend(Integer.parseInt(beginRecommend[i]));
	    	priceRecommend.setEndRecommend(Integer.parseInt(endRecommend[i]));
	    	priceRecommend.setPrice1Recommend(Double.parseDouble(price1Recommend[i]));
	    	recommendList.add(priceRecommend);
	    }
	    price.setResponderPriceList(responderList);
	    price.setRecommendPriceList(recommendList);
	    session.save(price);
	    session.getTransaction().commit();
		return price;
	}
	
	@SuppressWarnings("unchecked")
	public List<PriceList> getPriceListById_plan(int id_plan){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
	    session.getTransaction().begin();
	    
	    List<PriceList> priceList = new ArrayList<PriceList>();
	    priceList = session.createQuery("select s.id_price from SurveyPlan s LEFT JOIN s.id_price where s.id_plan=" + id_plan).list();
	   	    
	    session.getTransaction().commit();
		return priceList;
	}
	
}
